package com.djb.TvSite;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class VideoFiles {
	private static Process p;
	private static File currentVideo;
	public static List<File> getVideoFiles(File dir){
		List<File> fileNames = new ArrayList<File>();
		for(File f : dir.listFiles()){
			if(f.isDirectory()){
				for(File f2 : getVideoFiles(f)){
					fileNames.add(f2);
				}
			}
			else if(f.getName().toLowerCase().endsWith(".mp4")||f.getName().toLowerCase().endsWith(".mkv")||f.getName().toLowerCase().endsWith(".flv")||f.getName().toLowerCase().endsWith(".mpg")||f.getName().toLowerCase().endsWith(".avi")||f.getName().toLowerCase().endsWith(".wmv")){
				fileNames.add(f);
				
			}
			
		}
		Collections.sort(fileNames);
		return fileNames;
	}
	
	public static void playVideo(File video) throws IOException{
			String quit = "q";
			try {
				p.getOutputStream().write(quit.getBytes());
				p.getOutputStream().flush();
				p = Runtime.getRuntime().exec(new String[]{"omxplayer","-r","-ohdmi",video.getCanonicalPath()});
				currentVideo = video;
			} 
			catch(Exception e){
				p = Runtime.getRuntime().exec(new String[]{"omxplayer","-r","-ohdmi",video.getCanonicalPath()});
				currentVideo = video;
				
			}
		
	}
	public static void deleteVideo(File video){
		if(video.exists()){
			video.delete();
		}
	}
	
	public static File getCurrentVideo(){
		
		return currentVideo;
	}
	
	
	public static void sendCommand(String command) throws IOException{
		try{
			Command myCommand = Command.getCommand();
			String playerCommand = myCommand.getVideoCommands().get(command);
			p.getOutputStream().write(playerCommand.getBytes());
			p.getOutputStream().flush();
		}
		catch(Exception e){
			System.out.println("no video is currently playing");
		}
		
		
	}
	

}
